# -*- coding: utf-8 -*-
#from django.utils.translation import ugettext_lazy as _
import datastore_cache 
datastore_cache.DatastoreCachingShim.Install()

from google.appengine.ext import db
from django.contrib.auth.models import User 
#from moral.views import STUDENTS

BANFEI = 0

class Position(db.Model):
    title = db.StringProperty()
    describe = db.TextProperty()

    def __unicode__(self):
        return '%s' % (self.title)

    class Meta:
        verbose_name = u"职务"
        verbose_name_plural = u'职务'
        
class Student(db.Model):
    user = db.ReferenceProperty(User)
    name = db.StringProperty()
    sitenum = db.IntegerProperty(u"座号")
    phonenum = db.StringProperty(u"手机")
    position = db.ReferenceProperty(Position, verbose_name=u"职务")
    moral_pri_mark = db.FloatProperty(u"班德育分")   #本班德育分
    moral_pub_mark = db.FloatProperty(u"校德育分")   #政教处德育分
    boarder = db.BooleanProperty(u"是否寄宿", default=False)  #是否寄宿生
    coefficient = db.IntegerProperty(u"表现系数", default=3)    #表现系数
#    def id(self):
#        return self.key().id()
    
    def __unicode__(self):
        return '%s - %s' % (self.sitenum, self.user.first_name)
    
#    def change_moral(self,mark,op,type=1):
#        ml=Moral_Log()
#        ml.student=self
#        ml.operator=op
#        if type==1:
#            
            
    @classmethod
    def get_students(cls):
        query = Student.all()
        query.order('sitenum')
        students = query.fetch(100)
        return students
    
    class Meta:   
        ordering = ['sitenum']
        verbose_name = u"学生"
        verbose_name_plural = u'学生'        
        permissions = (
        ("can_change_moral_pri", u"可以更改班级德育分"),
        ("can_change_moral_pub", u"可以更改校德育分"),
        ("can_reset_userpwd", u"可以重设学生密码"),
        ("can_upload_users", u"可以上传用户文件"),
        ("can_manage_banfei", u"可以管理班费"),
        ("can_roll_call", u"可以点名、考勤"),
##        ("",""),             
        )


    
class Moral_Log(db.Model):
    student = db.ReferenceProperty(Student, verbose_name=u"学生 ")
    date = db.DateProperty(u"日期")
    type = db.IntegerProperty(u"类型")  #1、本班账户，2、学校
    reason = db.StringProperty(u"原因")
    mark = db.FloatProperty(u"分数")
    remark = db.StringProperty(u"备注")  #备注
    operator = db.ReferenceProperty(User, verbose_name=u"操作员", collection_name='op')
    addtime = db.DateTimeProperty(u"添加时间")
    
#    def id(self):
#        return self.key().id()
    
    def __unicode__(self):
        return '%s - %s  - %s - %s' % (self.date, self.student.user.first_name, self.reason, self.mark)
    
    class Meta:   
        ordering = ['date'] 
        verbose_name = u"德育分记录"
        verbose_name_plural = u'德育分记录'   
                
class Banfei_Log(db.Model):
    date = db.DateProperty()
    amount = db.FloatProperty()
    reason = db.StringProperty()
    agent = db.ReferenceProperty(Student)
    op = db.ReferenceProperty(User)

    def __unicode__(self):
        return '%s - %s  - %s - %s' % (self.date, self.reason, self.amount, self.agent.user.first_name)

    class Meta:   
        ordering = ['-date']
        verbose_name = u"班费记录"
        verbose_name_plural = u'班费记录'   
                
    @classmethod
    def get_banfei(cls):
        global BANFEI       
        if  BANFEI == 0:           
            bflogs = Banfei_Log.all()
            for bflog in bflogs:
                BANFEI += bflog.amount
        return BANFEI
        
class Day(db.Model):
    date = db.DateProperty()

#class Attendance(db.Model):
#    date=db.DateProperty()
#    order=db.IntegerProperty()
#    statu_normal=db.TextProperty()
#    statu_late=db.TextProperty()    #1、正常，2、迟到，3、旷课，4、早退，5、请假
#    statu_truant=db.TextProperty()
#    statu_leaveearly=db.TextProperty()
#    statu_leaving=db.TextProperty()
#
#    def __unicode__(self):
#        return '%s - %s  - %s - %s' % (self.lesson.date,str(self.lesson.NO), self.student.user.first_name,str(self.statu))
#    
#    class Meta:   
#        ordering = ['date']
#        verbose_name=u"考勤记录"
#        verbose_name_plural= u'考勤记录'   

class Lesson(db.Model):  #课程
    date = db.DateProperty(u"日期")
    order = db.IntegerProperty(u"节次")  
    ss=db.StringProperty(choices=set([u"第一节", u"dog", u"bird"]),verbose_name=u"节次")    
    
    def __unicode__(self):
        return '%s , 第  %s 节'%(self.date,self.order)    
    
    class Meta:
        verbose_name=u"课程"    
    
            
class Attendance(db.Model): #考勤
    lesson=db.ReferenceProperty(Lesson,verbose_name=u"课程")
    student = db.ReferenceProperty(Student, verbose_name=u"学生")
    statu = db.IntegerProperty(default=1, verbose_name=u"出勤情况")  #1、正常，2、迟到，3、旷课，4、早退，5、请假
    op = db.ReferenceProperty(User, verbose_name=u"操作员")
       
    def __unicode__(self):
        return '%s - %s  - %s - %s' % (self.lesson.date, str(self.lesson.NO), self.student.user.first_name, str(self.statu))
    
    class Meta:   
        ordering = ['date']
        verbose_name = u"考勤记录"
        verbose_name_plural = u'考勤记录'   
        
        
class Errancy(db.Model): #错误行为记录
    student=db.ReferenceProperty(Student,verbose_name=u"学生")
    
    class Meta:   
        verbose_name = u"错误行为记录"
        verbose_name_plural = u'错误行为记录'